<?php	
	if(isset($_POST['ajax']))
	{		
		$user = $_POST['user'];
		include("../../functions.php");
		//include("../../functions_shared.php");
		include("../connection.php");
	}
	
	//displaying all the user cicles
	echo '<div class="sidebar-box">';
	echo '<div class="sidebar-header">My Circles
	<span style="float:right" class="a" onclick="refresh_left_column(this)" id="leftColumnRefreshLink">
	<img src="images/refresh.png" style="width:20px">
	</span>
	</div>	
	<div class="sidebar-div" id="sidebar-div">';	
	
	//echo '<br><a id="new_circle_button" href="index.php?cat=group&action=create" class="button1"><span>New Circle</span></a>
	echo '<button id="new_circle_button" class="button">New Circle</button>
	
	<br><span class="small-font" style="color:#555">Create a new gausipp circle by adding friends from facebook. 
	<!--You can also <span onclick=\'sendRequestToRecipients("", "")\' class="a">invite friends</span> to gausipp.--></span>
	
	<br><br><br>';
	
	$res = mysql_query("SELECT gpid FROM site_group_members WHERE `fbid`='$user' AND `del`!='1'");
	if(!$res) echo mysql_error();	
	if(mysql_numrows($res) != 0)
	{		
		while($row = mysql_fetch_array($res))
		{
			$temp = $row['gpid'];
			$res1 = mysql_query("SELECT newness FROM site_group_newness WHERE `gpid`='$temp'");			
			if(mysql_numrows($res1) != 0) $newness = mysql_result($res1, 0, 'newness');
			else $newness = 0;
			$arr[] = array('newness' => $newness, 'gpid'=> $temp);
		}	
		
		
		// Our own custom comparison function
		// for sorting in decreasing order
			function fixem($a, $b){
			  if ($a["newness"] == $b["newness"]) { return 0; }
			  return ($a["newness"] < $b["newness"]) ? 1 : -1;
			}

			// Our Call to Sort the Data
			usort($arr, "fixem");
			
		//echo '<pre>';
		//print_r($arr);
		//echo '</pre>';
		
		foreach ($arr as $key => $val) {
				fetch_gp_details($val['gpid'],"link_".$user);		
			}
		
		//echo '<br>';
	}
	else
		echo "You don't have any circle yet.<br><br>";
		
	
		
		echo '</div></div>';
		
if(!isset($_POST['ajax']))
{
	if($_GET['cat'] == "group" && isset($_GET['gpid']))
	{		
		$gpid = $_GET['gpid'];
		if(isset($_GET['action']) && $_GET['action'] =="info")
		{
		?>
			<script>
			$(function() {
				$('#groupSettings_<?php echo $gpid; ?>_sidebarlink').click();
			});			
			</script>
		<?php
		}
		else
		{
		?>
			<script>
			$(function() {
				$('#group_<?php echo $gpid; ?>_sidebarlink').click();
			});			
			</script>
		<?php
		}
		
	}
}	
		
?>